package com.ruoyi.system.mc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mc.entity.BaseMsgEnum;
import com.ruoyi.system.mc.entity.McProduct;
import com.ruoyi.system.mc.entity.McProductDetailed;
import com.ruoyi.system.mc.entity.R;
import com.ruoyi.system.mc.mapper.McProductMapper;
import com.ruoyi.system.mc.service.McProductDetailedService;
import com.ruoyi.system.mc.service.McProductService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： McProductServiceImpl
 *  创建时间：2024/5/25
 */
@Service
public class McProductServiceImpl extends ServiceImpl<McProductMapper,McProduct> implements McProductService {

    //商品表Service层
    @Resource
    private McProductService mcProductService;

    //商品详情表Service层
    @Resource
    private McProductDetailedService mcProductDetailedService;

    /**
     * 查询商品数据集合
     * @param mcProduct 条件参数
     * @return 商品集合数据
     */
    @Override
    public R<McProduct> getProductList(McProduct mcProduct) {
        //页码
        Integer current = 0;
        //行数
        Integer size = 0;
        /**
         * 分页插件暂时没研究明白配置弄在哪
         */
        if (mcProduct.getCurrentPage()!=0){
            //页码赋值
            current = (mcProduct.getCurrentPage()-1)*mcProduct.getPageSize();
            //总行数赋值
            size = mcProduct.getPageSize();
        }
        //查询所有数据
        List<McProduct> productList = mcProductService.list(mcProduct.getLambdaQueryWrapper(mcProduct).last("limit " + current + "," +size ));
        //查询总行数
        Long total = mcProductService.count(mcProduct.getLambdaQueryWrapper(mcProduct));
        //返回统一返回格式类型
        return new R<>("查询成功",200,productList,total,current,size);
    }

    /**
     * 删除方法
     * @param productId 商品主键ID
     * @return 返回删除结果字符串
     */
    @Override
    public String deleteProductById(Long productId) {
        //判断productId不为null
        if (productId == null){
            return "删除失败";
        }else{ //否则正常执行删除
            //查询是否有子目录商品
            List<McProductDetailed> mcProductDetailedList = mcProductDetailedService.list(new LambdaQueryWrapper<McProductDetailed>()
                    //通过商品ID查询对象
                    .eq(McProductDetailed::getProductId, productId)
                    //逻辑删除为假
                    .eq(McProductDetailed::getDeleteFlag,Boolean.FALSE)
            );
            //判断查询到的详情数据是否为null
            if (mcProductDetailedList.size() == 0){
                //使用逻辑删除
                boolean update = mcProductService.update(new UpdateWrapper<McProduct>()
                        //设置逻辑删除为真
                        .lambda().set(McProduct::getDeleteFlag, Boolean.TRUE)
                        //通过ID查找
                        .eq(McProduct::getProductId, productId)
                );
                //返回操作结果
                return update == true ? BaseMsgEnum.SUCCESS.getGetMsg():BaseMsgEnum.ERROR.getGetMsg();
            }else{
                return "该商品存在详情数据请先删除商品详情数据";
            }
        }
    }

    /**
     * 修改操作
     * @param mcProduct 数据源对象
     * @return 返回操作结果字符串
     */
    @Override
    public String updateProductById(McProduct mcProduct) {
        //判断对象不为null
        if (mcProduct!=null){
            //执行修改
            Boolean update = mcProductService.updateById(mcProduct);
            //返回操作结果
            return update == true ? BaseMsgEnum.SUCCESS.getGetMsg():BaseMsgEnum.ERROR.getGetMsg();
        }else{
            //如果没有传递正确的对象则返回操作异常
            return BaseMsgEnum.ERROR.getGetMsg();
        }
    }


    /**
     * 新增操作
     * @param mcProduct 新增数据
     * @return 返回操作结果字符串
     */
    @Override
    public String insetProduct(McProduct mcProduct) {
        //判断增加对象不为null
        if (Objects.nonNull(mcProduct)){
            //创建日期格式对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //设置商品对应的商家ID
            mcProduct.setUserId(SecurityUtils.getLoginUser().getUserId());
            //设置一些默认值
            mcProduct.setCreateBy(SecurityUtils.getUsername());
            mcProduct.setCreateTime(java.sql.Date.valueOf(sdf.format(new Date())));
            mcProduct.setUpdateBy(SecurityUtils.getUsername());
            mcProduct.setUpdateTime(java.sql.Date.valueOf(sdf.format(new Date())));
            mcProduct.setDeleteFlag(Boolean.FALSE);
            //执行添加方法
            Boolean save = mcProductService.save(mcProduct);
            //返回提示结果字符串
            return save == true ? BaseMsgEnum.SUCCESS.getGetMsg() : BaseMsgEnum.ERROR.getGetMsg();
        }else{
            //返回操作失败提示
            return BaseMsgEnum.ERROR.getGetMsg();
        }
    }

    /**
     * 查询所有数据通过收藏数倒叙
     * @param mcProduct 条件对象
     * @return 返回结果集合
     */
    @Override
    public R<McProduct> selectAllProductByCollectCount(McProduct mcProduct) {
        //页码
        Integer current = 0;
        //行数
        Integer size = 0;
        /**
         * 分页插件暂时没研究明白配置弄在哪
         */
        if (mcProduct.getCurrentPage()!=0){
            //页码赋值
            current = (mcProduct.getCurrentPage()-1)*mcProduct.getPageSize();
            //总行数赋值
            size = mcProduct.getPageSize();
        }
        //查询所有数据
        List<McProduct> productList = mcProductService.list(mcProduct.getLambdaQueryWrapper(mcProduct).last("order by collect_count limit " + current + "," +size ));
        //查询总行数
        Long total = mcProductService.count(mcProduct.getLambdaQueryWrapper(mcProduct));
        //返回统一返回格式类型
        return new R<>("查询成功",200,productList,total,current,size);
    }


}
